package com.jie.eclipse.pan.editors;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.tigris.subversion.subclipse.ui.editor.RemoteFileEditorInput;

import com.jie.eclipse.pan.editors.editor.JSPEditor;


/**
 * An example showing how to create a multi-page editor.
 * This example has 3 pages:
 * <ul>
 * <li>page 0 contains a nested text editor.
 * <li>page 1 allows you to change the font used in page 2
 * <li>page 2 shows the words in page 0 in sorted order
 * </ul>
 */
public class PLLJspFormEditor extends BaseFormEditor{
	private JSPEditor uiEditor = null;
	private JSPEditor backEditor = null;
	private JSPEditor oldEditor = null;
	

	private boolean isFirst = true;
	
	public PLLJspFormEditor() {
		super();
		ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
	}
	
	/**
	 * Creates page 0 of the multi-page editor,
	 * which contains a text editor.
	 */
	public void createPage0() {
		if(getEditorInput() instanceof RemoteFileEditorInput){
			RemoteFileEditorInput re = (RemoteFileEditorInput)getEditorInput();
			StringBuffer sb = new StringBuffer();
			String line;
			final String newline = System.getProperty("line.separator");
			try {
				InputStream input = re.getStorage().getContents();
				BufferedReader reader = new BufferedReader(new InputStreamReader(
						input,"UTF-8"));
				
					while ((line = reader.readLine()) != null) {
						sb.append(line);
						sb.append(newline);
					}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			Text text = new Text(this.getContainer(), SWT.BORDER|SWT.V_SCROLL);
			text.setEditable(false);
			text.setText(sb.toString());
			int index = addPage(text);
			setPageText(index, "远程文件");
			
//			try {
//				int index = 0;
//				TextEditor text1 = new TextEditor();
//				IFile ifile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(re.getPath());
//				FileEditorInput fi = new FileEditorInput(ifile);
//				index = addPage(text1,fi);
//				setPageText(index, "远程文件");
//			} catch (PartInitException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
		}else{
			uiEditor = new JSPEditor();
			try {
				int index = addPage(uiEditor, getUiInput());
				setPageText(index, "UI源码");
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * Creates page 1 of the multi-page editor,
	 * which allows you to change the font used in page 2.
	 */
	public void createPage1() {
		backEditor = new JSPEditor();
		try {
			int index = addPage(backEditor, getBackInput());
			setPageText(index, "BACK源码");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	public void createPage2() {
		if(getOldInput() == null) return;
		oldEditor = new JSPEditor();
		try {
			int index = addPage(oldEditor, getOldInput());
			setPageText(index, "Old Jsp 源码");
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	protected void addPages() {
		createPage0();
		if(this.isPllJSP()){
			createPage1();
			createPage2();
		}
		
//		if(this.isBackInput())
//			this.setActivePage(1);
	}
	
	public void dispose() {
		ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
		super.dispose();
	}
	/**
	 * Saves the multi-page editor's document.
	 */
	public void doSave(IProgressMonitor monitor) {
		this.getActiveEditor().doSave(monitor);
	}
	
	public void doSaveAs() {
		IEditorPart editor = getEditor(0);
		editor.doSaveAs();
		setPageText(0, editor.getTitle());
		setInput(editor.getEditorInput());
	}
	
	public void gotoMarker(IMarker marker) {
		setActivePage(0);
		IDE.gotoMarker(getEditor(0), marker);
	}
	
	public void init(IEditorSite site, IEditorInput editorInput)
		throws PartInitException {
//		if (!(editorInput instanceof IFileEditorInput))
//			throw new PartInitException("Invalid Input: Must be IFileEditorInput");
		super.init(site, editorInput);
	}
	
	
	
	public boolean isSaveAsAllowed() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.jie.eclipse.pan.editors.BaseEditor#setFocus()
	 */
	@Override
	public void setFocus() {
		if(this.isBackInput() && this.isPllJSP() && this.isFirst){
			this.setActivePage(1);
			this.backEditor.setFocus();
			this.isFirst = false;
		}else{
			super.setFocus();
		}
	}
	
	@Override
	public String getPartName() {
		if(getEditorInput() instanceof RemoteFileEditorInput){
			RemoteFileEditorInput re1 = (RemoteFileEditorInput)getEditorInput();
			return re1.getName();
		}
		
		FileEditorInput fi = (FileEditorInput)getEditorInput();
		return fi.getName().replace(".jsp", "");
	}
	
	public String getName() {
		FileEditorInput fi = (FileEditorInput)getEditorInput();
		return fi.getName().replace(".jsp", "").replace("_backend", "");
	}
	
	public boolean isBackInput(){
		if(!(getEditorInput() instanceof FileEditorInput)){return false;}
		FileEditorInput fi = (FileEditorInput)getEditorInput();
		return fi.getName().endsWith("_backend.jsp");
	}
	
	public boolean isPllJSP(){
		if(!(getEditorInput() instanceof FileEditorInput)){return false;}
		return getUiInput().exists() && getBackInput().exists();
	}
	
	
	public FileEditorInput getBackInput() {
		if(this.backInput == null){
			if(this.isBackInput()){
				this.backInput = (FileEditorInput)getEditorInput();
			}else{
				FileEditorInput fi = (FileEditorInput)getEditorInput();
				String uiJsp = getName()+"_backend.jsp";
				IFolder f1 = (IFolder)fi.getFile().getParent();
				this.backInput = new FileEditorInput(f1.getFile(uiJsp));
			}
		}
		return backInput;
	}
	public FileEditorInput getUiInput(){
		
		if(this.uiInput == null){
			if(!this.isBackInput()){
				this.uiInput = (FileEditorInput)getEditorInput();
			}else{
				FileEditorInput fi = (FileEditorInput)getEditorInput();
				String uiJsp = getName()+".jsp";
				IFolder f1 = (IFolder)fi.getFile().getParent();
				this.uiInput = new FileEditorInput(f1.getFile(uiJsp));
			}
		}
		return uiInput;
	}
	
	public JSPEditor getUiEditor() {
		return uiEditor;
	}

	public JSPEditor getBackEditor() {
		return backEditor;
	}
	
	public JSPEditor getOldEditor() {
		return oldEditor;
	}
}
